class TablesController < ApplicationController
  include ApplicationHelper
  include TablesHelper
  def index
    @event = Event.find(params[:event_id])
    @dine = Dine.find(params[:dine_id])
    @tables = @dine.tables.paginate :page => params[:page], :per_page => 10

    @normal_table_users=get_normal_table_users(@dine)
    @top_table_users=get_top_table_users(@dine)

    #TODO 此处创建一个session，将user_hash缓存起来
    @user_hash = query_statistics(@dine)

    @normal_table_froms = get_normal_table_froms(@dine)

#    respond_to do |format|
#      format.html # index.html.erb
#      format.xml  { render :xml => @tables }
#    end
  end

  def query_tables
    arr = params[:id].split(',')
    dine = Dine.find(arr[0])
    if arr[1]
      @tables = dine.tables.paginate_all_by_type arr[1], :page => params[:page], :per_page => 5
    else
      @tables = dine.tables.paginate :page => params[:page], :per_page => 5
    end
    unless (params[:page])
      render :partial=>'table_list',:locals=>{:tables=>@tables}
    else
      render :update do |page|
        page.replace_html "show_tables",:partial=>'table_list',:locals=>{:tables=>@tables,:page=>params[:page]}
      end
    end
    
  end

  def create
    @table = Table.new(params[:table])
    @table.dine = Dine.find(params[:dine_id])
    @table.type=params[:table][:type]
    @table.save
  end

  def show_edit
    @table = Table.find(params[:id])
    render :partial=>'tables/edit_table_form',:locals=>{:table=>@table}
  end

  def update
    @table = Table.find(params[:id])
    @table.type=params[:table][:type] #因为这里采用的是类型，所以必须手动指定
    if @table.update_attributes(params[:table])
      render :update do |page|
        page.replace "table_#{@table.id}", :partial=>"tables/table_item",:locals=>{:table=>@table}
        page["table_#{@table.id}"].visual_effect :highlight
        page.replace_html("edit_place_#{@table.id}", '')
      end
    end
  end

  def delete
    table = Table.find(params[:id])
    del(table)
  end

  def pre_top_table_allocation
    @start_index=Level::OPTIONS.index(params[:table][:start_level])
    @end_index=Level::OPTIONS.index(params[:table][:end_level])
    @count = params[:table][:count]
    @dine = Dine.find(params[:dine_id])
    @users=get_users_by_level(@dine.event,@start_index,@end_index)
    @contain_users_count=@dine.top_tables.count * @count.to_i
  end

  def cancel_allocation
    render :text=>''
  end

  #分配主桌
  def allocation_top_table
    #需要找到用户
    dine=Dine.find(params[:dine_id])
    start_index = params[:start_index].to_i
    end_index=params[:end_index].to_i
    users=get_users_by_level(dine.event,start_index,end_index)

    #每个餐桌分配多少人
    count = params[:count].to_i

    tables=dine.top_tables
    tables.each do |table|
      eaters=[]
      count.times{
        user = users.pop
        if user
          eaters << user.id
          users.delete(user)
        end
      }
      table.eaters=eaters.join(',')
      table.save()
    end
    redirect_to event_dine_tables_path(:event_id=>dine.event.id,:dine_id=>dine.id)
  end

  #获取某个餐桌的就餐者
  def query_users
    table=Table.find(params[:id])
    users = table.users;
    render :partial=>'query_users',:locals=>{:users=>users,:table=>table}
  end
  
  def hide_edit
    render :text=>''
  end

  #将用户从餐桌中移除
  def remove_user
    arr = params[:id].split(',')
    table = Table.find(arr[0].to_i)
    eaters=table.eaters.split(',')
    eaters.delete(arr[1])
    table.eaters=eaters.join(',')
    if table.save
      render :update do |page|
        page.remove("user_#{arr[1]}")
        page.replace_html "#{table.id}_users_count",table.users_count
      end
    end
  end

  #分配批定单位的人到某个餐桌
  def allocation_from_to_table
    from = params[:from]
    source_table_id = params[:table].to_i
    if source_table_id >0
      source_table = NormalTable.find(source_table_id)
      source_arr = source_table.froms.split(',')
      source_arr.delete(from)
      source_table.froms = source_arr.join(',')
      source_table.save
    end
    table = NormalTable.find(params[:current_table])
    if table.froms
      arr = table.froms.split(',')
      unless arr.index(from)
        arr << from
        table.froms = arr.join(',')
      end
    else
      table.froms = from;
    end
    if table.save
      render :update do |page|
        unless source_table_id >0
          page.remove("users_by_#{from}")
        else
          page.remove("#{source_table_id}_user_from_#{from}");
        end
        page.insert_html :bottom,"completed_table_#{table.id}",:partial=>'completed_allocation_item',:locals=>{:id=>table.id,:key=>from,:user_hash=>query_statistics(table.dine)}
      end
    end
  end

  def allocation_table_to_from
    from = params[:from]
    table = NormalTable.find(params[:table])
    arr=table.froms.split(',')
    arr.delete(from)
    table.froms=arr.join(',')
    if table.save
      render :update do |page|
        page.remove("#{table.id}_user_from_#{from}")
        page.insert_html :bottom,"pending_froms",:partial=>'pending_allocation_item',:locals=>{:key=>from,:user_hash=>query_statistics(table.dine)}
      end
    end
  end

  #确认预分配的计划
  def allocation_normal_table
    dine = Dine.find(params[:id])
    dine.normal_tables.each do |table|
      users=[]
      if table.froms
        table.froms.split(',').each do |from|
          if query_statistics(dine).key?(from)
            users += query_statistics(dine)[from].collect{|user| user.id}
          end
        end
      end
      table.eaters=users.join(',')
      table.froms=''
      table.save
    end
    redirect_to event_dine_tables_path(:event_id=>dine.event.id,:dine_id=>dine.id)
  end



private
  def del(obj)
    if obj.destroy
      render :update do |page|
        page.remove("table_#{obj.id}")
      end
    end
  end
end
